using System;
using System.Windows.Forms;
using System.Drawing;
using TeachingManagementSystem.Models;
using TeachingManagementSystem.Forms.UserManagement;
using TeachingManagementSystem.Forms.CourseManagement;
using TeachingManagementSystem.Forms.ClassroomManagement;
using TeachingManagementSystem.Forms.ScheduleManagement;
using TeachingManagementSystem.BLL;

namespace TeachingManagementSystem
{
    public partial class MainForm : Form
    {
        private User _currentUser;
        private UserBLL _userBLL;
        private CourseBLL _courseBLL;
        private ScheduleBLL _scheduleBLL;

        public MainForm(User currentUser)
        {
            InitializeComponent();
            _currentUser = currentUser;
            _userBLL = new UserBLL();
            _courseBLL = new CourseBLL();
            _scheduleBLL = new ScheduleBLL();
            
            // 设置窗口大小
            this.WindowState = FormWindowState.Maximized;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            
            InitializeForm();
        }

        private void InitializeForm()
        {
            // 设置欢迎信息
            lblWelcome.Text = $"欢迎，{_currentUser.FullName}！角色：{_currentUser.Role} | 最后登录：{DateTime.Now.ToShortDateString()}";
            // 根据用户角色配置菜单
            ConfigureMenuByRole();
            // 添加主页面内容
            AddHomePageContent();
        }

        private void AddHomePageContent()
        {
            // 清除现有内容（除了菜单和状态栏）
            this.Controls.Clear();
            this.Controls.Add(menuStrip1);
            this.Controls.Add(statusStrip1);

            // 创建主内容面板
            Panel mainPanel = new Panel();
            mainPanel.Dock = DockStyle.Fill;
            mainPanel.Padding = new Padding(20);
            mainPanel.BackColor = Color.WhiteSmoke;
            this.Controls.Add(mainPanel);

            // 创建顶部用户信息面板
            Panel userInfoPanel = CreateUserInfoPanel();
            userInfoPanel.Dock = DockStyle.Top;
            mainPanel.Controls.Add(userInfoPanel);

            // 创建快速访问面板的容器
            FlowLayoutPanel topContainer = new FlowLayoutPanel();
            topContainer.Dock = DockStyle.Top;
            topContainer.FlowDirection = FlowDirection.LeftToRight;
            topContainer.WrapContents = false;
            topContainer.Margin = new Padding(0, 20, 0, 0);
            mainPanel.Controls.Add(topContainer);

            // 添加快速访问面板
            Panel quickAccessPanel = CreateQuickAccessPanel();
            quickAccessPanel.Width = 350;
            topContainer.Controls.Add(quickAccessPanel);

            // 添加最近活动面板
            Panel recentActivitiesPanel = CreateRecentActivitiesPanel();
            recentActivitiesPanel.Dock = DockStyle.Fill;
            recentActivitiesPanel.Margin = new Padding(0, 20, 0, 0);
            mainPanel.Controls.Add(recentActivitiesPanel);
        }

        private Panel CreateUserInfoPanel()
        {
            Panel panel = new Panel();
            panel.Height = 80;
            panel.BackColor = Color.White;
            panel.BorderStyle = BorderStyle.FixedSingle;
            panel.Padding = new Padding(20);

            // 用户信息标签
            Label userLabel = new Label();
            userLabel.Text = $"{_currentUser.FullName}";
            userLabel.Font = new Font("Microsoft YaHei", 16, FontStyle.Bold);
            userLabel.Location = new Point(20, 10);
            userLabel.AutoSize = true;

            Label roleLabel = new Label();
            roleLabel.Text = $"角色：{_currentUser.Role} | 用户ID：{_currentUser.UserID}";
            roleLabel.Font = new Font("Microsoft YaHei", 9, FontStyle.Regular);
            roleLabel.ForeColor = Color.Gray;
            roleLabel.Location = new Point(20, 40);
            roleLabel.AutoSize = true;

            panel.Controls.Add(userLabel);
            panel.Controls.Add(roleLabel);

            return panel;
        }

        private Panel CreateQuickAccessPanel()
        {
            Panel panel = new Panel();
            panel.Height = 200;
            panel.BackColor = Color.White;
            panel.BorderStyle = BorderStyle.FixedSingle;
            panel.Padding = new Padding(15);

            Label titleLabel = new Label();
            titleLabel.Text = "快速访问";
            titleLabel.Font = new Font("Microsoft YaHei", 12, FontStyle.Bold);
            titleLabel.Location = new Point(15, 15);
            titleLabel.AutoSize = true;

            panel.Controls.Add(titleLabel);

            // 创建快速访问按钮
            string[][] quickActions = GetQuickActionsByRole();
            int y = 50;

            foreach (string[] action in quickActions)
            {
                Button button = new Button();
                button.Text = action[0];
                button.Width = 300;
                button.Height = 45;
                button.Location = new Point(20, y);
                button.Font = new Font("Microsoft YaHei", 11);
                button.BackColor = Color.LightSkyBlue;
                button.ForeColor = Color.White;
                button.FlatStyle = FlatStyle.Flat;
                button.Click += (sender, e) => HandleQuickAction(action[1]);
                panel.Controls.Add(button);
                y += 60;
            }

            return panel;
        }

        private string[][] GetQuickActionsByRole()
        {
            switch (_currentUser.Role)
            {
                case UserRole.Administrator:
                    return new string[][] {
                        new string[] {"用户管理", "UserList"},
                        new string[] {"课程管理", "CourseList"},
                        new string[] {"教室管理", "ClassroomList"}
                    };
                case UserRole.Teacher:
                    return new string[][] {
                        new string[] {"我的课程", "CourseList"},
                        new string[] {"排课管理", "ScheduleList"},
                        new string[] {"调整课表", "AddSchedule"},
                        new string[] {"修改密码", "ChangePassword"}
                    };
                case UserRole.Student:
                    return new string[][] {
                        new string[] {"查询课表", "ScheduleList"},
                        new string[] {"预约教室", "ClassroomList"},
                        new string[] {"修改密码", "ChangePassword"}
                    };
                case UserRole.Staff:
                    return new string[][] {
                        new string[] {"用户管理", "UserList"},
                        new string[] {"教室管理", "ClassroomList"},
                        new string[] {"课表管理", "ScheduleList"}
                    };
                default:
                    return new string[][] {
                        new string[] {"修改密码", "ChangePassword"}
                    };
            }
        }

        private void HandleQuickAction(string action)
        {
            switch (action)
            {
                case "UserList":
                    menuUserList_Click(null, EventArgs.Empty);
                    break;
                case "CourseList":
                    menuCourseList_Click(null, EventArgs.Empty);
                    break;
                case "ClassroomList":
                    menuClassroomList_Click(null, EventArgs.Empty);
                    break;
                case "ScheduleList":
                    menuScheduleList_Click(null, EventArgs.Empty);
                    break;
                case "AddSchedule":
                    menuAddSchedule_Click(null, EventArgs.Empty);
                    break;
                case "ChangePassword":
                    menuChangePassword_Click(null, EventArgs.Empty);
                    break;
            }
        }




        private Panel CreateRecentActivitiesPanel()
        {
            Panel panel = new Panel();
            panel.BackColor = Color.White;
            panel.BorderStyle = BorderStyle.FixedSingle;
            panel.Padding = new Padding(15);

            Label titleLabel = new Label();
            titleLabel.Text = "最近活动";
            titleLabel.Font = new Font("Microsoft YaHei", 12, FontStyle.Bold);
            titleLabel.Location = new Point(15, 15);
            titleLabel.AutoSize = true;

            panel.Controls.Add(titleLabel);

            // 创建活动列表
            ListBox activityListBox = new ListBox();
            activityListBox.Dock = DockStyle.Fill;
            activityListBox.Margin = new Padding(15, 50, 15, 15);
            activityListBox.Font = new Font("Microsoft YaHei", 9);
            activityListBox.HorizontalScrollbar = true;

            // 添加默认活动项
            activityListBox.Items.AddRange(new string[] {
                $"[{DateTime.Now.ToShortTimeString()}] 您已成功登录系统",
                $"[{DateTime.Today.AddHours(-2).ToShortTimeString()}] 系统已更新至最新版本",
                $"[{DateTime.Today.AddDays(-1).ToShortDateString()}] 教学计划已发布",
                $"[{DateTime.Today.AddDays(-2).ToShortDateString()}] 新的教室资源已添加",
                $"[{DateTime.Today.AddDays(-3).ToShortDateString()}] 学期日程表已更新",
                $"[{DateTime.Today.AddDays(-4).ToShortDateString()}] 课程安排已优化",
                $"[{DateTime.Today.AddDays(-5).ToShortDateString()}] 用户权限设置更新"
            });

            panel.Controls.Add(activityListBox);

            return panel;
        }

        private void ConfigureMenuByRole()
        {
            // 默认禁用所有管理菜单
            menuUserManagement.Enabled = false;
            menuCourseManagement.Enabled = false;
            menuClassroomManagement.Enabled = false;
            menuScheduleManagement.Enabled = false;

            // 根据角色启用相应菜单
            switch (_currentUser.Role)
            {
                case UserRole.Administrator:
                    // 管理员可以访问所有功能
                    menuUserManagement.Enabled = true;
                    menuCourseManagement.Enabled = true;
                    menuClassroomManagement.Enabled = true;
                    menuScheduleManagement.Enabled = true;
                    break;
                case UserRole.Teacher:
                    // 教师可以进行排课和调整课表
                    menuCourseManagement.Enabled = true;
                    menuScheduleManagement.Enabled = true;
                    break;
                case UserRole.Student:
                    // 学生可以预约教室和查询课表
                    menuClassroomManagement.Enabled = true;
                    menuScheduleManagement.Enabled = true;
                    break;
                case UserRole.Staff:
                    // 行政人员可以管理用户、教室和课表
                    menuUserManagement.Enabled = true;
                    menuClassroomManagement.Enabled = true;
                    menuScheduleManagement.Enabled = true;
                    break;
            }
        }

        private void InitializeComponent()
        {
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.menuUserManagement = new System.Windows.Forms.ToolStripMenuItem();
            this.menuUserList = new System.Windows.Forms.ToolStripMenuItem();
            this.menuAddUser = new System.Windows.Forms.ToolStripMenuItem();
            this.menuCourseManagement = new System.Windows.Forms.ToolStripMenuItem();
            this.menuCourseList = new System.Windows.Forms.ToolStripMenuItem();
            this.menuAddCourse = new System.Windows.Forms.ToolStripMenuItem();
            this.menuClassroomManagement = new System.Windows.Forms.ToolStripMenuItem();
            this.menuClassroomList = new System.Windows.Forms.ToolStripMenuItem();
            this.menuAddClassroom = new System.Windows.Forms.ToolStripMenuItem();
            this.menuScheduleManagement = new System.Windows.Forms.ToolStripMenuItem();
            this.menuScheduleList = new System.Windows.Forms.ToolStripMenuItem();
            this.menuAddSchedule = new System.Windows.Forms.ToolStripMenuItem();
            this.menuPersonal = new System.Windows.Forms.ToolStripMenuItem();
            this.menuChangePassword = new System.Windows.Forms.ToolStripMenuItem();
            this.menuLogout = new System.Windows.Forms.ToolStripMenuItem();
            this.menuAbout = new System.Windows.Forms.ToolStripMenuItem();
            this.statusStrip1 = new System.Windows.Forms.StatusStrip();
            this.lblWelcome = new System.Windows.Forms.ToolStripStatusLabel();
            this.menuStrip1.SuspendLayout();
            this.statusStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // menuStrip1
            // 
            this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.menuUserManagement,
            this.menuCourseManagement,
            this.menuClassroomManagement,
            this.menuScheduleManagement,
            this.menuPersonal,
            this.menuAbout});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(800, 28);
            this.menuStrip1.TabIndex = 0;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // menuUserManagement
            // 
            this.menuUserManagement.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.menuUserList,
            this.menuAddUser});
            this.menuUserManagement.Name = "menuUserManagement";
            this.menuUserManagement.Size = new System.Drawing.Size(102, 24);
            this.menuUserManagement.Text = "用户管理";
            // 
            // menuUserList
            // 
            this.menuUserList.Name = "menuUserList";
            this.menuUserList.Size = new System.Drawing.Size(136, 26);
            this.menuUserList.Text = "用户列表";
            this.menuUserList.Click += new System.EventHandler(this.menuUserList_Click);
            // 
            // menuAddUser
            // 
            this.menuAddUser.Name = "menuAddUser";
            this.menuAddUser.Size = new System.Drawing.Size(136, 26);
            this.menuAddUser.Text = "添加用户";
            this.menuAddUser.Click += new System.EventHandler(this.menuAddUser_Click);
            // 
            // menuCourseManagement
            // 
            this.menuCourseManagement.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.menuCourseList,
            this.menuAddCourse});
            this.menuCourseManagement.Name = "menuCourseManagement";
            this.menuCourseManagement.Size = new System.Drawing.Size(102, 24);
            this.menuCourseManagement.Text = "课程管理";
            // 
            // menuCourseList
            // 
            this.menuCourseList.Name = "menuCourseList";
            this.menuCourseList.Size = new System.Drawing.Size(136, 26);
            this.menuCourseList.Text = "课程列表";
            this.menuCourseList.Click += new System.EventHandler(this.menuCourseList_Click);
            // 
            // menuAddCourse
            // 
            this.menuAddCourse.Name = "menuAddCourse";
            this.menuAddCourse.Size = new System.Drawing.Size(136, 26);
            this.menuAddCourse.Text = "添加课程";
            this.menuAddCourse.Click += new System.EventHandler(this.menuAddCourse_Click);
            // 
            // menuClassroomManagement
            // 
            this.menuClassroomManagement.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.menuClassroomList,
            this.menuAddClassroom});
            this.menuClassroomManagement.Name = "menuClassroomManagement";
            this.menuClassroomManagement.Size = new System.Drawing.Size(102, 24);
            this.menuClassroomManagement.Text = "教室管理";
            // 
            // menuClassroomList
            // 
            this.menuClassroomList.Name = "menuClassroomList";
            this.menuClassroomList.Size = new System.Drawing.Size(136, 26);
            this.menuClassroomList.Text = "教室列表";
            this.menuClassroomList.Click += new System.EventHandler(this.menuClassroomList_Click);
            // 
            // menuAddClassroom
            // 
            this.menuAddClassroom.Name = "menuAddClassroom";
            this.menuAddClassroom.Size = new System.Drawing.Size(136, 26);
            this.menuAddClassroom.Text = "添加教室";
            this.menuAddClassroom.Click += new System.EventHandler(this.menuAddClassroom_Click);
            // 
            // menuScheduleManagement
            // 
            this.menuScheduleManagement.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.menuScheduleList,
            this.menuAddSchedule});
            this.menuScheduleManagement.Name = "menuScheduleManagement";
            this.menuScheduleManagement.Size = new System.Drawing.Size(102, 24);
            this.menuScheduleManagement.Text = "课表管理";
            // 
            // menuScheduleList
            // 
            this.menuScheduleList.Name = "menuScheduleList";
            this.menuScheduleList.Size = new System.Drawing.Size(136, 26);
            this.menuScheduleList.Text = "课表列表";
            this.menuScheduleList.Click += new System.EventHandler(this.menuScheduleList_Click);
            // 
            // menuAddSchedule
            // 
            this.menuAddSchedule.Name = "menuAddSchedule";
            this.menuAddSchedule.Size = new System.Drawing.Size(136, 26);
            this.menuAddSchedule.Text = "添加课表";
            this.menuAddSchedule.Click += new System.EventHandler(this.menuAddSchedule_Click);
            // 
            // menuPersonal
            // 
            this.menuPersonal.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.menuChangePassword,
            this.menuLogout});
            this.menuPersonal.Name = "menuPersonal";
            this.menuPersonal.Size = new System.Drawing.Size(67, 24);
            this.menuPersonal.Text = "个人";
            // 
            // menuChangePassword
            // 
            this.menuChangePassword.Name = "menuChangePassword";
            this.menuChangePassword.Size = new System.Drawing.Size(166, 26);
            this.menuChangePassword.Text = "修改密码";
            this.menuChangePassword.Click += new System.EventHandler(this.menuChangePassword_Click);
            // 
            // menuLogout
            // 
            this.menuLogout.Name = "menuLogout";
            this.menuLogout.Size = new System.Drawing.Size(166, 26);
            this.menuLogout.Text = "退出登录";
            this.menuLogout.Click += new System.EventHandler(this.menuLogout_Click);
            // 
            // menuAbout
            // 
            this.menuAbout.Name = "menuAbout";
            this.menuAbout.Size = new System.Drawing.Size(67, 24);
            this.menuAbout.Text = "关于";
            this.menuAbout.Click += new System.EventHandler(this.menuAbout_Click);
            // 
            // statusStrip1
            // 
            this.statusStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
            this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.lblWelcome});
            this.statusStrip1.Location = new System.Drawing.Point(0, 427);
            this.statusStrip1.Name = "statusStrip1";
            this.statusStrip1.Size = new System.Drawing.Size(800, 26);
            this.statusStrip1.TabIndex = 1;
            this.statusStrip1.Text = "statusStrip1";
            // 
            // lblWelcome
            // 
            this.lblWelcome.Name = "lblWelcome";
            this.lblWelcome.Size = new System.Drawing.Size(50, 20);
            this.lblWelcome.Text = "欢迎";
            // 
            // MainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            // 移除固定大小设置，让窗口可以最大化
            this.StartPosition = FormStartPosition.CenterScreen;
            this.WindowState = FormWindowState.Maximized;
            this.FormBorderStyle = FormBorderStyle.Sizable;
            this.MaximizeBox = true;
            this.MinimizeBox = true;
            this.Controls.Add(this.statusStrip1);
            this.Controls.Add(this.menuStrip1);
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "MainForm";
            this.Text = "教学管理系统 - 主界面";
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.statusStrip1.ResumeLayout(false);
            this.statusStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        // 用户管理菜单事件
        private void menuUserList_Click(object? sender, EventArgs e)
        {
            UserListForm userListForm = new UserListForm();
            userListForm.ShowDialog();
        }

        private void menuAddUser_Click(object? sender, EventArgs e)
        {
            UserEditForm userEditForm = new UserEditForm(new User());
            userEditForm.ShowDialog();
        }

        // 课程管理菜单事件
        private void menuCourseList_Click(object? sender, EventArgs e)
        {
            CourseListForm courseListForm = new CourseListForm();
            courseListForm.ShowDialog();
        }

        private void menuAddCourse_Click(object? sender, EventArgs e)
        {
            CourseEditForm courseEditForm = new CourseEditForm(new Course());
            courseEditForm.ShowDialog();
        }

        // 教室管理菜单事件
        private void menuClassroomList_Click(object? sender, EventArgs e)
        {
            ClassroomListForm classroomListForm = new ClassroomListForm(_currentUser);
            classroomListForm.ShowDialog();
        }

        private void menuAddClassroom_Click(object? sender, EventArgs e)
        {
            ClassroomEditForm classroomEditForm = new ClassroomEditForm(new Classroom());
            classroomEditForm.ShowDialog();
        }

        // 课表管理菜单事件
        private void menuScheduleList_Click(object? sender, EventArgs e)
        {
            ScheduleListForm scheduleListForm = new ScheduleListForm(_currentUser);
            scheduleListForm.ShowDialog();
        }

        private void menuAddSchedule_Click(object? sender, EventArgs e)
        {
            ScheduleEditForm scheduleEditForm = new ScheduleEditForm(new Schedule(), _currentUser);
            scheduleEditForm.ShowDialog();
        }

        // 个人菜单事件
        private void menuChangePassword_Click(object? sender, EventArgs e)
        {
            // 打开修改密码表单
            Forms.UserManagement.ChangePasswordForm changePasswordForm = new Forms.UserManagement.ChangePasswordForm(_currentUser);
            changePasswordForm.ShowDialog();
        }

        private void menuLogout_Click(object? sender, EventArgs e)
        {
            if (MessageBox.Show("确定要退出登录吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void menuAbout_Click(object? sender, EventArgs e)
        {
            MessageBox.Show("教学管理系统 v1.0\n\n用于管理教学相关信息的系统", "关于", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private System.Windows.Forms.MenuStrip menuStrip1;
        private System.Windows.Forms.ToolStripMenuItem menuUserManagement;
        private System.Windows.Forms.ToolStripMenuItem menuUserList;
        private System.Windows.Forms.ToolStripMenuItem menuAddUser;
        private System.Windows.Forms.ToolStripMenuItem menuCourseManagement;
        private System.Windows.Forms.ToolStripMenuItem menuCourseList;
        private System.Windows.Forms.ToolStripMenuItem menuAddCourse;
        private System.Windows.Forms.ToolStripMenuItem menuClassroomManagement;
        private System.Windows.Forms.ToolStripMenuItem menuClassroomList;
        private System.Windows.Forms.ToolStripMenuItem menuAddClassroom;
        private System.Windows.Forms.ToolStripMenuItem menuScheduleManagement;
        private System.Windows.Forms.ToolStripMenuItem menuScheduleList;
        private System.Windows.Forms.ToolStripMenuItem menuAddSchedule;
        private System.Windows.Forms.ToolStripMenuItem menuPersonal;
        private System.Windows.Forms.ToolStripMenuItem menuChangePassword;
        private System.Windows.Forms.ToolStripMenuItem menuLogout;
        private System.Windows.Forms.ToolStripMenuItem menuAbout;
        private System.Windows.Forms.StatusStrip statusStrip1;
        private System.Windows.Forms.ToolStripStatusLabel lblWelcome;
    }
}